package com.waves.chat.controller;


import com.alibaba.fastjson.JSON;
import com.waves.chat.domain.dto.chat.ChatMessageMarkDTO;
import com.waves.chat.domain.pojo.msg.MsgReadInfoDTO;
import com.waves.chat.domain.dto.chat.*;
import com.waves.chat.domain.vo.ChatMessageReadVO;
import com.waves.chat.domain.vo.ChatMessageVO;
import com.waves.chat.service.ChatService;
import com.waves.core.annotation.FrequencyControl;
import com.waves.core.utils.LoginUtils;
import com.waves.core.web.domain.R;
import com.waves.core.web.domain.vo.CursorPageBaseVO;
import com.waves.user.cache.UserCache;
import com.waves.core.domain.enums.BlackTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * 群聊相关接口
 * </p>
 *
 * @author dlw
 * @since 2024-03-19
 */
@RestController
@RequestMapping("/userApi/chat")
@Api(tags = "聊天室相关接口")
@Slf4j
public class ChatController {
    @Resource
    private ChatService chatService;
    @Resource
    private UserCache userCache;

    private Set<String> getBlackUidSet() {
        return userCache.getBlackMap().getOrDefault(BlackTypeEnum.UID.getType(), new HashSet<>());
    }

    @GetMapping("/public/msg/page")
    @ApiOperation("消息列表")
    public R<CursorPageBaseVO<ChatMessageVO>> getMsgPage(@Valid ChatMessagePageDTO request) {
        CursorPageBaseVO<ChatMessageVO> msgPage = chatService.getMsgPage(request);
        filterBlackMsg(msgPage);
        return R.ok(msgPage);
    }

    private void filterBlackMsg(CursorPageBaseVO<ChatMessageVO> memberPage) {
        Set<String> blackMembers = getBlackUidSet();
        memberPage.getList().removeIf(a -> blackMembers.contains(a.getFromUser().getUid().toString()));
    }

    @PostMapping("/msg")
    @ApiOperation("发送消息")
    @FrequencyControl(time = 5, count = 10, target = FrequencyControl.Target.UID)
    public R<ChatMessageVO> sendMsg(@Valid @RequestBody ChatMessageDTO request) {
        log.info("发送的消息内容:{}", JSON.toJSONString(request));
        Long msgId = chatService.sendMsg(request,LoginUtils.getUserId());
        //返回完整消息格式，方便前端展示
        return R.ok(chatService.getMsgResp(msgId, LoginUtils.getUserId()));
    }

    @PutMapping("/msg/mark")
    @ApiOperation("消息标记")
    public R<Void> setMsgMark(@Valid @RequestBody ChatMessageMarkDTO request) {
        chatService.setMsgMark(request);
        return R.ok();
    }

    @PutMapping("/msg/recall")
    @ApiOperation("撤回消息")
    public R<Void> recallMsg(@Valid @RequestBody ChatMessageBaseDTO request) {
        chatService.recallMsg(request);
        return R.ok();
    }

    @GetMapping("/msg/read/page")
    @ApiOperation("消息的已读未读列表")
    public R<CursorPageBaseVO<ChatMessageReadVO>> getReadPage(@Valid ChatMessageReadDTO request) {
        return R.ok(chatService.getReadPage(request));
    }

    @GetMapping("/msg/read")
    @ApiOperation("获取消息的已读未读总数")
    public R<Collection<MsgReadInfoDTO>> getReadInfo(@Valid ChatMessageReadInfoDTO request) {
        return R.ok(chatService.getMsgReadInfo(request));
    }

    @PutMapping("/msg/read")
    @ApiOperation("消息阅读上报")
    public R<Void> msgRead(@Valid @RequestBody ChatMessageMemberDTO request) {
        chatService.msgRead(request);
        return R.ok();
    }
}

